/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Interfaces.ApparentModalI;
import Model.Artist;
import Model.Musicgroupmember;
import java.awt.Frame;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import javax.swing.JFrame;
import src.General;

/**
 * Πίνακας με τους διαθέσιμους καλλιτέχνες για προσθήκη σε συγκρότημα
 *
 */
public class AddArtistToGroupJFrame extends javax.swing.JFrame implements ApparentModalI {

    General.ActionType actionType;
    private DiaxeirishGroupJFrame caller;
    private final JFrame next = null;
    private List<Artist> artistList;

    /**
     * Creates new form AddArtistToGroupJFrame
     */
    public AddArtistToGroupJFrame() {
         throw new UnsupportedOperationException();
    }

    /**
     * constructor overload
     *
     * @param caller
     */
    public AddArtistToGroupJFrame(DiaxeirishGroupJFrame caller) {
        initComponents();
        this.caller = caller;
        try {
            General.setWindowImage(this);
        } catch (IOException ex) {
        }
        this.setFormData();
        General.setJTableNotEditable(jTable1);
        this.jTable1.getRowSorter().toggleSortOrder(0);
    }

    public boolean isTableEmpty() {
        return this.artistList.size() == 0;
    }

    public void setFormData() {
        this.artistList = General.runNamedQuery("Artist.findAll", Artist.class, this.caller.getEm());
        //Αφαιρεί τους καλλιτέχνες που υπάρχουν στο συγκρότημα
        Artist current;
        List<Artist> callerList = this.caller.getArtistList();
        for (Iterator<Artist> it = this.artistList.iterator(); it.hasNext();) {
            current = it.next();
            for (int i = 0; i < this.caller.getArtistList().size(); i++) {
                if (Objects.equals(current.getArtistId(), callerList.get(i).getArtistId())) {
                    it.remove();
                    break;
                }
            }
        }
        this.artistList = General.bindTableToObservableArtistList(artistList, jTable1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Εισαγωγή καλλιτέχνη");
        setMinimumSize(new java.awt.Dimension(887, 393));

        saveButton.setText("Εισαγωγή");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        jButton2.setText("Ακύρωση");
        jButton2.setActionCommand("");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Διαθέσιμοι καλλιτέχνες"));

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 791, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(16, 16, 16))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (!General.checkRowSelected(jTable1, "καλλιτέχνης", "καλλιτέχνη", this)) {
            return;
        }
        Artist selected = this.artistList.get(General.getSelectedRowModelIndex(jTable1));
        Musicgroupmember mgm;
        mgm = new Musicgroupmember();
        mgm.setArtistId(selected);
        mgm.setMusicGroupId(this.caller.getGroup());
        this.caller.getGroupMembers().add(mgm);
        this.caller.getArtistList().add(selected);
        if (this.caller.getArtistList().size() == 0) {
            this.caller.disableDelete();
        } else {
            this.caller.enableDelete();
        }
        General.selectModelRowNVisible(this.caller.getArtistList().indexOf(selected), this.caller.getTable());
        General.closeWindowX(this);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
      General.closeWindowX(this);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddArtistToGroupJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddArtistToGroupJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddArtistToGroupJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddArtistToGroupJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddArtistToGroupJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

    @Override
    public Frame getCaller() {
        return this.caller;
    }

    @Override
    public Frame getNext() {
        return this.next;
    }

    @Override
    public void setCallersNextNull() {
        this.caller.setNext(null);
    }

    @Override
    public void setNext(Frame w) {
        throw new UnsupportedOperationException("modal:error"); //To change body of generated methods, choose Tools | Templates.
    }
}
